package sweep;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serial;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.io.*;
import 

public class Menu extends JFrame {

    private static DataOutputStream out;
    private String name = "";
    private Timer timer;
    
    public Menu(Socket client) {
        JLabel RivalLabel = new JLabel("Rival_Num:");
        JTextField RivalNum = new JTextField(10);
        JButton InviteButton = new JButton("Invite");
        timer = new Timer(100, new TimerHandler(client));
        timer.start();
        InviteButton.addActionListener(e -> {
            if (RivalNum.getText().equals("")) {
                JOptionPane.showMessageDialog(null,
                        "Please enter the rival's num",
                        "Error", JOptionPane.ERROR_MESSAGE);
            } 
            else {
                if (name.equals("player" + RivalNum.getText())) {
                    JOptionPane.showMessageDialog(null,
                        "You can't invite yourself!",
                        "Error", JOptionPane.ERROR_MESSAGE);
                }
                else {
                    try {
                        out = new DataOutputStream(client.getOutputStream());
                        out.writeUTF("invite player" + RivalNum.getText());
                    } catch (IOException ee) {
                        ee.printStackTrace();
                        return;
                    }
                }
            }
        });
        RivalLabel.setBounds(50, 30, 200 , 25);
        RivalNum.setBounds(50, 60, 200, 25);
        InviteButton.setBounds(50, 90, 200, 25);
        add(RivalLabel);
        add(RivalNum);
        add(InviteButton);
        add(new JLabel(""));
    }

    // 处理 Server 消息
    class TimerHandler implements ActionListener {

        Socket client;

        public TimerHandler(Socket client) {this.client = client;}

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            try {
                String msg = Client.getMessage();
                String[] message = msg.split(" ");
                // System.out.println(msg);
                if (message[0].equals("num") == true) {
                    name = "player" + message[1];
                    System.out.println("Your name: " + name);
                }
                // 邀请的对象不存在
                else if (message[0].equals("rival_unexist") == true) {
                    JOptionPane.showMessageDialog(null,
                        "The player is not exist or online.",
                        "Error", JOptionPane.ERROR_MESSAGE);
                }
                // 某人邀请你进行游戏
                else if (message[0].equals("invite") == true) {
                    String rival = message[1];
                    System.out.println("invite");
                    Sweep.startJoinBoard(client, rival, name);
                    dispose();
                }
                // 对方接受邀请
                else if (message[0].equals("invite_yes") == true) {
                    String rival = message[1];
                    Sweep.startStartBoard(client, rival);
                    dispose();
                }
                // 对方拒绝邀请
                else if (message[0].equals("invite_no") == true) {
                    JOptionPane.showMessageDialog(null,
                        "The person reject your invitation.",
                        "Error", JOptionPane.ERROR_MESSAGE);
                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    public static void main(String[] args) {
        Socket client = new Socket();
        String rival = "";
        Sweep.startGameBoard(client, rival);        
    }
}
